package com.ly.hotel.view.setting;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.google.gson.JsonObject;
import com.ly.hotel.R;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.util.ToastUtil;
import com.ly.hotel.webagent.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by SHUBEN on 2018/4/16.
 */

public class WifiLayout extends LinearLayout {

    private Context context;
    private MyApplication app;
    private Button submitWifi;
    private EditText wifiName;
    private EditText wifiPwd;
    private Handler handler;

    public WifiLayout(Context c) {
        super(c);
        this.context = c;
        this.app = (MyApplication)context.getApplicationContext();
        LayoutInflater.from(getContext()).inflate(R.layout.layout_wifiset, this);
        initView();
    }

    private void initView(){
        submitWifi = findViewById(R.id.submit_wifi);
        wifiName = findViewById(R.id.wifi_name_edit);
        wifiPwd = findViewById(R.id.wifi_pwd_edit);
        wifiName.setText(app.wifiName);
        wifiPwd.setText(app.wifiPwd);
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 0){
                    app.wifiName = wifiName.getText().toString();
                    app.wifiPwd = wifiPwd.getText().toString();
                    ToastUtil.showShortToast(context,"设置成功",3000);
                }else{
                    ToastUtil.showShortToast(context,"失败",3000);
                }
            }
        };
        submitWifi.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!wifiName.getText().toString().equals(app.wifiName) ||
                        !wifiPwd.getText().toString().equals(app.wifiPwd)){
                    setWifiMsg(wifiName.getText().toString(),wifiPwd.getText().toString());
                }
            }
        });
    }

    //存储wifi信息
    private void setWifiMsg(final String wifi,final String pwd){
        new Thread(new Runnable() {
            @Override
            public void run() {
                String URL = MyApplication.getHostResUrl() + "/api/dbfile-write.json";
                JsonObject areaObj = new JsonObject();
                areaObj.addProperty("wifi", wifi);
                areaObj.addProperty("password", pwd);
                String request = "filename=WifiInfo&content=" + areaObj.toString();
                Log.i("httpGet", URL + "?" + request);
                String sendRes = HttpRequest.sendGet(URL, request);
                try {
                    JSONObject jsobj = new JSONObject(sendRes);
                    int code = jsobj.getInt("code");
                    if (code == 200) {
                        handler.sendEmptyMessage(0);
                    }else{
                        handler.sendEmptyMessage(1);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
